package com.example.controller;

import com.example.common.Result;
import com.example.entity.StudyAreaTime;
import com.example.service.StudyAreaTimeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 帖子前端接口
 */
@RestController
@RequestMapping("/studyareatime")
public class StudyAreaTimeController {

    @Resource
    private StudyAreaTimeService studyAreaTimeService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody StudyAreaTime studyAreaTime) {
        studyAreaTimeService.add(studyAreaTime);
        return Result.success();
    }

    @PostMapping("/addByCondition")
    public Result addByCondition(@RequestBody StudyAreaTime studyAreaTime) {
        // 根据uid查询用户数据
        List<StudyAreaTime> existingLearningrange =  studyAreaTimeService.selectAllByCondition(studyAreaTime);

        if (existingLearningrange != null) {
            studyAreaTimeService.updateByCondition(studyAreaTime);
        } else {
            // 如果不存在该uid的用户数据，则执行新增操作
            studyAreaTimeService.add(studyAreaTime);
        }

        return Result.success();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        studyAreaTimeService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        studyAreaTimeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody StudyAreaTime studyAreaTime) {
        studyAreaTimeService.updateById(studyAreaTime);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        StudyAreaTime studyAreaTime = studyAreaTimeService.selectById(id);
        return Result.success(studyAreaTime);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll( StudyAreaTime studyAreaTime) {
        List<StudyAreaTime> studyAreaTimeList = studyAreaTimeService.selectAll(studyAreaTime);
        return Result.success(studyAreaTimeList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(StudyAreaTime studyAreaTime,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<StudyAreaTime> pageInfo = studyAreaTimeService.selectPage(studyAreaTime, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
